package com.clss.emergencycall.module.imui.adapter.holder;

import android.os.Handler;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.clss.emergencycall.R;
import com.clss.emergencycall.base.Constant;
import com.clss.emergencycall.utils.Lg;
import com.clss.emergencycall.utils.PublicUtils;
import com.clss.emergencycall.utils.StrUtils;
import com.clss.emergencycall.utils.Tools;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.clss.emergencycall.module.imui.adapter.ChatAdapter;
import com.clss.emergencycall.module.imui.enity.MessageInfo;
import com.clss.emergencycall.module.imui.util.IMUIConstants;
import com.clss.emergencycall.module.imui.util.Utils;
import com.clss.emergencycall.module.imui.widget.BubbleImageView;
import com.clss.emergencycall.module.imui.widget.BubbleLinearLayout;
import com.clss.emergencycall.module.imui.widget.GifTextView;

/**
 * 作者：Rance on 2016/11/29 10:47
 * 邮箱：rance935@163.com
 */
public  class ChatSendViewHolder extends BaseViewHolder<MessageInfo> {
    private static final String TAG = "ChatRightViewHolder";

    TextView chatItemDate;
    ImageView chatItemHeader;
    GifTextView chatItemContentText;
    BubbleImageView chatItemContentImage;
    ImageView chatItemFail;
    ProgressBar chatItemProgress;
    ImageView chatItemVoice;
    BubbleLinearLayout chatItemLayoutContent;
    TextView chatItemVoiceTime;

    private ChatAdapter.onItemClickListener onItemClickListener;
    private Handler handler;
    private RelativeLayout.LayoutParams layoutParams;

    public ChatSendViewHolder(ViewGroup parent, ChatAdapter.onItemClickListener onItemClickListener, Handler handler) {
        super(parent, R.layout.imui_item_chat_send);
        chatItemDate = itemView.findViewById(R.id.chat_item_date);
        chatItemHeader = itemView.findViewById(R.id.chat_item_header);
        chatItemContentText = itemView.findViewById(R.id.chat_item_content_text);
        chatItemContentImage = itemView.findViewById(R.id.chat_item_content_image);
        chatItemFail = itemView.findViewById(R.id.chat_item_fail);
        chatItemProgress = itemView.findViewById(R.id.chat_item_progress);
        chatItemVoice = itemView.findViewById(R.id.chat_item_voice);
        chatItemLayoutContent = itemView.findViewById(R.id.chat_item_layout_content);
        chatItemVoiceTime = itemView.findViewById(R.id.chat_item_voice_time);

        this.onItemClickListener = onItemClickListener;
        this.handler = handler;
        layoutParams = (RelativeLayout.LayoutParams) chatItemLayoutContent.getLayoutParams();
    }


    @Override
    public void setData(MessageInfo data) {
        chatItemDate.setText(data.getTime() != null ? data.getTime() : "");
        Glide.with(getContext()).load(data.getHeader()).into(chatItemHeader);
        if (data.getContent() != null) {
            String strContent = data.getContent().trim();
            strContent = StrUtils.ToDBC(strContent);
            chatItemContentText.setSpanText(handler,strContent, true);
            chatItemVoice.setVisibility(View.GONE);
            chatItemContentText.setVisibility(View.VISIBLE);
            chatItemLayoutContent.setVisibility(View.VISIBLE);
            chatItemVoiceTime.setVisibility(View.GONE);
            chatItemContentImage.setVisibility(View.GONE);
            TextPaint paint = chatItemContentText.getPaint();
            // 计算textview在屏幕上占多宽
            int len = (int) paint.measureText(chatItemContentText.getText().toString().trim());
            if (len < Utils.dp2px(getContext(), 200)){
                layoutParams.width = len + Utils.dp2px(getContext(), 30);
            } else {
                layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
            }
            int length = getLength(len,225);
            int hright = (int)(paint.getFontMetrics().bottom - paint.getFontMetrics().top) *length
                    + Utils.dp2px(getContext(), 20);
            layoutParams.height = hright;
            chatItemLayoutContent.setLayoutParams(layoutParams);
        } else if (data.getImageUrl() != null) {
            Lg.i(TAG, "---data.getImageUrl() != null===");

            chatItemVoice.setVisibility(View.GONE);
            chatItemLayoutContent.setVisibility(View.GONE);
            chatItemVoiceTime.setVisibility(View.GONE);
            chatItemContentText.setVisibility(View.GONE);
            chatItemContentImage.setVisibility(View.VISIBLE);
            if (data.getImageUrl().contains("http")){
                Tools.showUrlImage(getContext(), data.getImageUrl(), chatItemContentImage);
            }else {
                Glide.with(getContext()).load(data.getImageUrl()).into(chatItemContentImage);
            }
            chatItemContentImage.setOnClickListener(v ->
                    onItemClickListener.onImageClick(chatItemContentImage, getDataPosition()));
            layoutParams.width = Utils.dp2px(getContext(), 120);
            layoutParams.height = Utils.dp2px(getContext(), 48);
            chatItemLayoutContent.setLayoutParams(layoutParams);
        } else if (data.getMessageType() == Constant.TYPE_VOICE) {
            Lg.i(TAG, "---data.getFilepath() != null===");
            chatItemVoice.setVisibility(View.VISIBLE);
            chatItemLayoutContent.setVisibility(View.VISIBLE);
            chatItemContentText.setVisibility(View.GONE);
            chatItemVoiceTime.setVisibility(View.VISIBLE);
            chatItemContentImage.setVisibility(View.GONE);
            chatItemVoiceTime.setText(Utils.formatTime(data.getVoiceTime()));
            chatItemLayoutContent.setOnClickListener(v -> onItemClickListener.onVoiceClick(chatItemVoice, getDataPosition()));
            layoutParams.width = Utils.dp2px(getContext(), 120);
            layoutParams.height = Utils.dp2px(getContext(), 48);
            chatItemLayoutContent.setLayoutParams(layoutParams);
        }else if (data.getMessageType() == Constant.TYPE_VIDEO){
            chatItemVoice.setVisibility(View.GONE);
            chatItemLayoutContent.setVisibility(View.GONE);
            chatItemVoiceTime.setVisibility(View.GONE);
            chatItemContentText.setVisibility(View.GONE);
            chatItemContentImage.setVisibility(View.VISIBLE);
            chatItemContentImage.setBackgroundResource(R.color.black);
            chatItemContentImage.setImageResource(R.mipmap.video_back_play);
            chatItemContentImage.setOnClickListener(v -> {
                String file = data.getFilepath();
                PublicUtils.openVideo(getContext(),file);
            });
        }
        switch (data.getSendState()) {
            case IMUIConstants.CHAT_ITEM_SENDING:
                chatItemProgress.setVisibility(View.VISIBLE);
                chatItemFail.setVisibility(View.GONE);
                break;
            case IMUIConstants.CHAT_ITEM_SEND_ERROR:
                chatItemProgress.setVisibility(View.GONE);
                chatItemFail.setVisibility(View.VISIBLE);
                break;
            case IMUIConstants.CHAT_ITEM_SEND_SUCCESS:
                chatItemProgress.setVisibility(View.GONE);
                chatItemFail.setVisibility(View.GONE);
                break;
                default:
        }
    }

    private int getLength(int len,int maxWidth) {
        int lag  = Utils.dp2px(getContext(), maxWidth);
        return len/lag + 1;
    }

}
